package com.ryskoo.basicdata.weixin.repository;

import com.ryskoo.basicdata.weixin.domain.WeixinParamPO;
import com.ryskoo.common.constant.CacheConstant;
import com.ryskoo.framework.core.repository.BasicRedisImpl;
import com.ryskoo.framework.core.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;

/**
 * @author BeHappy
 */
@Primary
@Repository
public class WeixinParamRedisImpl extends BasicRedisImpl<WeixinParamPO> implements IWeixinParamRepository {
    
	@Autowired
    private RedisUtil redisClient;

    @Autowired
    private IWeixinParamRepository weixinParamRepository;

    @PostConstruct
    public void init() {
        this.setRedisClient(redisClient);
        this.setDao(weixinParamRepository);
    }

    public WeixinParamRedisImpl() {
        super(CacheConstant.BDS_WEIXIN_PARAM_KEY);
    }

    /**
     * 通过商户id获取商户微信配置
     *
     * @param vendorId 商户id
     * @return 商户微信配置
     */
    @Override
    public WeixinParamPO getByVendorId(String vendorId) {
        WeixinParamPO weixinParam = (WeixinParamPO) redisClient.getObject(CacheConstant.BDS_WEIXIN_PARAM_KEY + vendorId);
        if (null != weixinParam) {
            return weixinParam;
        }
        weixinParam = weixinParamRepository.getByVendorId(vendorId);
        if (null != weixinParam) {
            redisClient.setObject(CacheConstant.BDS_WEIXIN_PARAM_KEY + vendorId, weixinParam);
        }
        return weixinParam;
    }

    /**
     * 通过微信支付商户号获取商户微信配置
     *
     * @param payMchid 微信支付商户号
     * @return 商户微信配置
     */
    @Override
    public WeixinParamPO getByMchId(String payMchid) {
        WeixinParamPO weixinParam = (WeixinParamPO) redisClient.getObject(CacheConstant.BDS_WEIXIN_PARAM_KEY + payMchid);
        if (null != weixinParam) {
            return weixinParam;
        }
        weixinParam = weixinParamRepository.getByMchId(payMchid);
        if (null != weixinParam) {
            redisClient.setObject(CacheConstant.BDS_WEIXIN_PARAM_KEY + payMchid, weixinParam);
        }
        return weixinParam;
    }
}
